﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RPGOnline.Controls.Map.Layers.Obstruction.Editors
{
    public partial class ObstructionEditorForm : Form
    {
        private readonly MapCell m_mapCell;
        private ObstructionLine m_obstructionLine;

        public ObstructionEditorForm(MapTileset tileset, MapCell mapCell, int x, int y)
        {
            m_mapCell = mapCell;
            InitializeComponent();

            obstructionEditorControl1.OnPixelClicked += OnPixelClicked;

            UpdateLines();
            obstructionEditorControl1.Update(tileset, mapCell, x, y);
        }

        private void UpdateLines()
        {
            treeView1.Nodes[0].Nodes.Clear();

            int index = 0;
            foreach (ObstructionLine obstructionLine in m_mapCell.ObstructionLines)
            {
                TreeNode treeNode = treeView1.Nodes[0].Nodes.Add("Line " + (index++));
                treeNode.Tag = obstructionLine;
            }

            treeView1.ExpandAll();
        }

        private void OnPixelClicked(object sender, PixelClickedEventArgs e)
        {
            if (e.MapCell.ObstructionLines.Count == 0 || m_obstructionLine == null)
                return;

            Point scaledPoint = new Point(e.Pixel.X, e.Pixel.Y);

            m_obstructionLine.AddPoint(scaledPoint);
            obstructionEditorControl1.Invalidate();
        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            m_mapCell.ObstructionLines.Add(new ObstructionLine());
            UpdateLines();
            SelectLine(m_mapCell.ObstructionLines.Last());
        }

        private void SelectLine(ObstructionLine obstructionLine)
        {
            foreach (TreeNode treeNode in treeView1.Nodes[0].Nodes)
            {
                if(treeNode.Tag == obstructionLine)
                {
                    treeView1.SelectedNode = treeNode;
                    m_obstructionLine = obstructionLine;
                    obstructionEditorControl1.HighlightedLine = m_obstructionLine;
                    obstructionEditorControl1.Invalidate();
                    return;
                }
            }
        }

        private void tsbReverse_Click(object sender, EventArgs e)
        {
            if (m_obstructionLine == null)
                return;
            m_obstructionLine.Points.Reverse();
            obstructionEditorControl1.Invalidate();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            SelectLine(e.Node.Tag as ObstructionLine);
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            m_mapCell.ObstructionLines.Remove(m_obstructionLine);
            m_obstructionLine = null;

            if(m_mapCell.ObstructionLines.Count != 0)
                SelectLine(m_mapCell.ObstructionLines.Last());
        }
    }
}
